package com.yituo.mjworld.mvc.action.admin;

import com.yituo.mjworld.core.annotation.Auth;
import com.yituo.mjworld.core.annotation.Check;
import com.yituo.mjworld.core.annotation.Rule;
import com.yituo.mjworld.core.web.core.MyHttpRequest;
import com.yituo.mjworld.core.web.core.MyHttpResponse;
import com.yituo.mjworld.mvc.action.IBaseAction;
import com.yituo.mjworld.mvc.entity.web.WebSearchConf;
import com.yituo.mjworld.mvc.form.DeleteForm;
import com.yituo.mjworld.mvc.form.QueryForm;
import com.yituo.mjworld.mvc.service.impl.SearchServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 快捷搜索的模块
 */
@Component
public class SearchAction implements IBaseAction {
  @Autowired
  SearchServiceImpl searchService;

  @Override
  @Check(form = QueryForm.class)
  public void getList(MyHttpRequest req, MyHttpResponse res) throws IOException {
    QueryForm form = (QueryForm) req.getForm();
    res.success(searchService.getList(form));
  }

  @Override
  @Check(form = DeleteForm.class)
  public void delete(MyHttpRequest req, MyHttpResponse res) throws IOException {
    DeleteForm form = (DeleteForm) req.getForm();
    res.setResult(searchService.delete(form));
  }

  @Override
  @Check(form = WebSearchConf.class)
  public void saveOrUpdate(MyHttpRequest req, MyHttpResponse res) throws IOException {
    WebSearchConf form = (WebSearchConf) req.getForm();
    res.setResult(searchService.saveOrUpdate(form));
  }

  /**
   * 统计点击的次数
   *
   * @param req
   * @throws IOException
   * @aram res
   */
  @Rule(name = "id", name_zn = "热搜Id", min = "0")
  public void clickCount(MyHttpRequest req, MyHttpResponse res) throws IOException {
    int id = Integer.parseInt(req.getParameter("id"));
    res.setResult(searchService.clickCount(id));
  }

  //TODO:排序
}
